define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pt/salary/index' + location.search,
                    add_url: 'pt/salary/add',
                    edit_url: 'pt/salary/edit',
                    del_url: 'pt/salary/del',
                    multi_url: 'pt/salary/multi',
                    import_url: 'pt/salary/import',
                    table: 'pt_person_salary',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'duty_id', title: __('Duty_id')},
                        {field: 'date_id', title: __('Date_id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'pt_person_id', title: __('Pt_person_id')},
                        {field: 'pt_depart_id', title: __('Pt_depart_id')},
                        {field: 'depart_name', title: __('Depart_name'), operate: 'LIKE'},
                        {field: 'pt_job_id', title: __('Pt_job_id')},
                        {field: 'job_name', title: __('Job_name'), operate: 'LIKE'},
                        {field: 'clock_in', title: __('Clock_in')},
                        {field: 'clock_out', title: __('Clock_out')},
                        {field: 'stage_total', title: __('Stage_total')},
                        {field: 'salary_mode', title: __('Salary_mode'), searchList: {"hour":__('Salary_mode hour'),"day":__('Salary_mode day'),"stage":__('Salary_mode stage')}, formatter: Table.api.formatter.normal},
                        {field: 'salary_rule', title: __('Salary_rule'), operate: 'LIKE'},
                        {field: 'salary', title: __('Salary'), operate:'BETWEEN'},
                        {field: 'added1_fee', title: __('Added1_fee'), operate:'BETWEEN'},
                        {field: 'added2_fee', title: __('Added2_fee'), operate:'BETWEEN'},
                        {field: 'road_fee', title: __('Road_fee'), operate:'BETWEEN'},
                        {field: 'self_sevice_fee', title: __('Self_sevice_fee'), operate:'BETWEEN'},
                        {field: 'salary_payable', title: __('Salary_payable'), operate:'BETWEEN'},
                        {field: 'require_duration', title: __('Require_duration'), operate:'BETWEEN'},
                        {field: 'require_calc_duration', title: __('Require_calc_duration'), operate:'BETWEEN'},
                        {field: 'actual_duration', title: __('Actual_duration'), operate:'BETWEEN'},
                        {field: 'actual_calc_duration', title: __('Actual_calc_duration'), operate:'BETWEEN'},
                        {field: 'reward_month', title: __('Reward_month'), operate:'BETWEEN'},
                        {field: 'salary_total', title: __('Salary_total'), operate:'BETWEEN'},
                        {field: 'createtime', title: __('Createtime')},
                        {field: 'updatetime', title: __('Updatetime')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        statistic: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pt/salary/statistic' + location.search,
                    add_url: 'pt/salary/add',
                    //edit_url: 'pt/salary/edit',
                    //del_url: 'pt/salary/del',
                    multi_url: 'pt/salary/multi',
                    import_url: 'pt/salary/import',
                    table: 'pt_person_salary',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'pk',
                sortName: 'pk',
                fixedColumns: true,
                fixedRightNumber: 1,
                fixedNumber: 5,
                pagination: false,
                height: 700,
                columns: [
                    [
                        {field: 'labor_name', title: __('劳务公司')},
                        {field: 'user_type', title: __('人员类别'), searchList: {"self":__('自招兼职'),"intern":__('实习生'),"labor":__('劳务公司')}, formatter: Table.api.formatter.normal},
                        {field: 'depart_name', title: __('所属部门')},
                        {field: 'job_name', title: __('岗位')},
                        {field: 'user_name', title: __('姓名')},
                        {field: 'phone', title: __('联系电话')},
                        {field: 'id_card', title: __('身份证号')},
                        {field: 'bank_code', title: __('银行卡号')},
                        {field: 'bank_openname', title: __('开户行名称')},

                        {field: 'day_1', title: __('1号')},
                        {field: 'day_2', title: __('2号')},
                        {field: 'day_3', title: __('3号')},
                        {field: 'day_4', title: __('4号')},
                        {field: 'day_5', title: __('5号')},
                        {field: 'day_6', title: __('6号')},
                        {field: 'day_7', title: __('7号')},
                        {field: 'day_8', title: __('8号')},
                        {field: 'day_9', title: __('9号')},

                        {field: 'day_10', title: __('10号')},
                        {field: 'day_11', title: __('11号')},
                        {field: 'day_12', title: __('12号')},
                        {field: 'day_13', title: __('13号')},
                        {field: 'day_14', title: __('14号')},
                        {field: 'day_15', title: __('15号')},
                        {field: 'day_16', title: __('16号')},
                        {field: 'day_17', title: __('17号')},
                        {field: 'day_18', title: __('18号')},
                        {field: 'day_19', title: __('19号')},

                        {field: 'day_20', title: __('20号')},
                        {field: 'day_21', title: __('21号')},
                        {field: 'day_22', title: __('22号')},
                        {field: 'day_23', title: __('23号')},
                        {field: 'day_24', title: __('24号')},
                        {field: 'day_25', title: __('25号')},
                        {field: 'day_26', title: __('26号')},
                        {field: 'day_27', title: __('27号')},
                        {field: 'day_28', title: __('28号')},
                        {field: 'day_29', title: __('29号')},
                        {field: 'day_30', title: __('30号')},
                        {field: 'day_31', title: __('31号')},

                        {field: 'total_actual_duration', title: __('实际出勤天数/小时数/场次')},
                        {field: 'total_added1_fee', title: __('餐补')},
                        {field: 'total_added2_fee', title: __('补助')},
                        {field: 'total_road_fee', title: __('路费')},
                        {field: 'total_sevice_fee', title: __('服务费（自招人员）')},
                        {field: 'total_reward_month', title: __('激励补贴')},
                        {field: 'total_salary_total', title: __('应发总金额')},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                          field: 'operate', 
                          title: __('Operate'), 
                          table: table, 
                          events: Table.api.events.operate, 
                          buttons: [
                              {
                                  name: 'planwork',
                                  text: __('薪资详情'),
                                  icon: 'fa',
                                  classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                  url: function (row) {
                                      return Fast.api.fixurl('pt/salary/detail/ids/' + row.pk)
                                  },
                                  extend: "data-area='[\"80%\", \"80%\"]'",
                              },
                          ],

                          formatter: Table.api.formatter.operate

                        }
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                commonSearch: false,
                search: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $(".btn-embossed").on("click", function() {
                queryDate = $("#query_date").val();
                    // console.log(tt.bootstrapTable('getOptions'))
                table.bootstrapTable('refresh', {
                  url: 'pt/salary/statistic?query_date=' + queryDate
                })
            })
            $(".btn-export").on("click", function() {
                queryDate = $("#query_date").val();
                Fast.api.ajax({
                    url:'pt/salary/export?query_date=' + queryDate
                }, function(res){
                    console.log('export =>', res)
                    window.location.href=res['file']
                }, function(){
                    console.log('导出失败');
                    Backend.api.toastr.error('导出失败');
                });
            })
            Form.api.bindevent($("form[role=form]"));
        },
        detail: function () {
            // 初始化表格参数配置
            var ids = window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1);
            Table.api.init({
                extend: {
                    index_url: 'pt/salary/detail/ids/' + ids + location.search,
                    // add_url: 'pt/salary/add',
                    edit_url: 'pt/salary/edit',
                    // del_url: 'pt/salary/del',
                    multi_url: 'pt/salary/multi',
                    import_url: 'pt/salary/import',
                    table: 'pt_person_salary',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {field: 'id', title: __('Id'), visible: false},
                        {field: 'date', title: __('日期 ')},
                        {field: 'clock', title: __('打卡时间')},
                        {field: 'require_duration', title: __('应出勤时长')},
                        {field: 'require_calc_duration', title: __('应出计薪时长')},
                        {field: 'actual_duration', title: __('实际出勤时长')},
                        {field: 'actual_calc_duration', title: __('实际计薪时长')},
                        {field: 'salary', title: __('计薪规则')},
                        {field: 'added1_fee', title: __('Added1_fee'), operate:'BETWEEN'},
                        {field: 'added2_fee', title: __('Added2_fee'), operate:'BETWEEN'},
                        {field: 'road_fee', title: __('Road_fee'), operate:'BETWEEN'},
                        {field: 'salary_total', title: __('Salary_total'), operate:'BETWEEN'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                commonSearch: false,
                search: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
